#include "defs.h"
#include "data.h"
#include "decl.h"

/*
 * Given two primitive types, return true if they are compatible,
 * false otherwise. Also return either zero or an A_WIDEN operation
 * if one has to be widened to match the other. If onlyright is true,
 * only widen left to match right.
 */
int type_compatible(int *left_type, int *right_type, int onlyright)
{
    /* voids are not compatible with anything */
    if (*left_type == P_VOID || *right_type == P_VOID)
        return 0;

    /* same type (compatible) and no need to wide */
    if (*left_type == *right_type) {
        *left_type = *right_type = 0;
        return 1;
    }

    if (*left_type == P_CHAR && *right_type == P_INT) {
        *left_type = A_WIDEN;
        *right_type = 0;
        return 1;
    }

    if (*left_type == P_INT && *right_type == P_CHAR) {
        if (onlyright)
            return 0;
        *left_type = 0;
        *right_type = A_WIDEN;
        return 1;
    }
}
